package com.zhl.media.app.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.widget.Toast;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.muddzdev.styleabletoast.StyleableToast;
import com.orhanobut.hawk.Hawk;
import com.zhl.media.app.R;
import com.zhl.media.app.di.component.DaggerMainComponent;
import com.zhl.media.app.di.module.MainModule;
import com.zhl.media.app.model.MediaPhoto;
import com.zhl.media.app.mvp.contract.MainContract;
import com.zhl.media.app.mvp.presenter.MainPresenter;
import com.zhl.media.app.mvp.ui.fragment.MineFragment;
import com.zhl.media.app.mvp.ui.fragment.TaskFragment;
import com.zhl.media.app.utils.EntityManager;
import com.zhl.media.app.utils.TabEntity;
import com.zhl.media.app.utils.TagAliasOperatorHelper;
import com.zhl.media.commonres.base.AppActivity;
import com.zhl.media.commonsdk.core.EventBusCode;
import com.zhl.media.commonsdk.core.RouterHub;
import com.zhl.media.commonsdk.data.UserUtil;
import com.zhl.media.commonsdk.utils.DeleteFileUtil;
import com.zhl.media.commonsdk.utils.EmptyUtils;
import com.zhl.media.commonsdk.utils.VersionUtils;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.zhl.media.app.utils.TagAliasOperatorHelper.ACTION_SET;

@Route(path = RouterHub.APP_MAINACTIVITY)
public class MainActivity extends AppActivity<MainPresenter> implements MainContract.View {

    private ArrayList<Fragment> allFragments;
    private String[] mTitles = {"任务", "我的"};
    private int[] mIconUnselectIds = {R.drawable.task_select, R.drawable.mine_select};
    private int[] mIconSelectIds = {R.drawable.task_unselect, R.drawable.mine_unselect};
    private long mPressedTime;
    @BindView(R.id.tabBar)
    CommonTabLayout mTabLayout_2;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMainComponent.builder().appComponent(appComponent).mainModule(new MainModule(this)).build().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ARouter.getInstance().inject(this);
        if(EmptyUtils.isNotEmpty(Hawk.get("version"))){
            if(!Hawk.get("version").equals(VersionUtils.getVersionName(this))){
                Hawk.put("version",VersionUtils.getVersionName(this));
                EntityManager.getInstance(this).getMediaPhotoDao().deleteAll();
                EntityManager.getInstance(this).getProjDao().deleteAll();
                EntityManager.getInstance(this).getElevatorDao().deleteAll();
                EntityManager.getInstance(this).getMediaPositionDao().deleteAll();
                EntityManager.getInstance(this).getAdJustDao().deleteAll();
                DeleteFileUtil.deleteDirectory(this.getCacheDir().getPath());
            }
        }else {
            Hawk.put("version",VersionUtils.getVersionName(this));
        }
        mPresenter.getThisDate();
    }

    private void setAlias() {
        TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
        tagAliasBean.action = ACTION_SET;
        tagAliasBean.alias = UserUtil.getUser().getId();
        tagAliasBean.isAliasAction = true;
        TagAliasOperatorHelper.getInstance().handleAction(getApplicationContext(), 1, tagAliasBean);
    }

    private void initViews() {
        mTabEntities.add(new TabEntity(mTitles[0], mIconSelectIds[0], mIconUnselectIds[0]));
        mTabEntities.add(new TabEntity(mTitles[1], mIconSelectIds[1], mIconUnselectIds[1]));
        allFragments = new ArrayList<>();
        allFragments.add(TaskFragment.newInstance());
        allFragments.add(MineFragment.newInstance());
        mTabLayout_2.setTabData(mTabEntities,this,R.id.fl_tab_container,allFragments);
        setAlias();
        loadTags();
    }


    // 更新待上传数
    @Subscriber(tag = "update_dsc_num")
    public void update_dsc_num(int code) {
        if(code == EventBusCode.DSCNUM){
            loadTags();
        }
    }

    public void loadTags(){
        List<MediaPhoto> mediaPhotos = EntityManager.getInstance(this).getMediaPhotoDao().loadAll();
        if(mediaPhotos.size()>0){
            mTabLayout_2.showMsg(1,mediaPhotos.size());
            mTabLayout_2.setMsgMargin(1, -7, 5);
        }else {
            mTabLayout_2.hideMsg(1);
        }
    }

    @Override
    public void onBackPressed() {
        //获取第一次按键时间
        long mNowTime = System.currentTimeMillis();
        //比较两次按键时间差
        if ((mNowTime - mPressedTime) > 2000) {
            StyleableToast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_LONG, R.style.mytoast).show();
            mPressedTime = mNowTime;
        } else {
            ArmsUtils.exitApp();
            super.onBackPressed();
        }
    }

    @Override
    public void showLoading() {
        showDiglog();
    }

    @Override
    public void hideLoading() {
        hideDiglog();
    }

    @Override
    public void showMessage(@NonNull String message) {

    }

    @Override
    public void launchActivity(@NonNull Intent intent) {

    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onThisDate(String date) {
        Hawk.put("thisDate",date);
        initViews();
    }

}
